package cn.kgc.crowd1048.fundingadmin.common;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

/**
 * SpringSecurity框架
 * 权限控制  配置类
 */
@EnableGlobalMethodSecurity(prePostEnabled=true)//允许在方法上定义安全约束,而不仅仅是基于URL或请求参数
@EnableWebSecurity
public class MySecutityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    UserDetailsService userDetailsService;


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()//对请求进行授权
                .antMatchers("/bootstrap/**",
                        "/css/**",
                        "/fonts/**",
                        "/img/**",
                        "/jquery/**",
                        "/layer/**",
                        "/script/**",
                        "/ztree/**",
                        "/fundingadmin/t-admin/index")//使用ANT风格设置要授权的URL地址
                .permitAll()//允许上面使用ANT风格设置的全部请求
                //设置权限 方式一
//                .antMatchers("/admin/query/for/search")
//                .hasAnyRole("董事长","超级管理员")

                .anyRequest()//其他未设置的全部请求
                .authenticated()//需要认证
                .and()
                .formLogin()//设置未授权请求跳转到登录页面
                .loginPage("/fundingadmin/t-admin/admin/to/login/page.html")//指定登录页面
                .permitAll()//为登录页面设置所有人可以访问
                .loginProcessingUrl("/admin/secutity/do/login.html")//指定登录处理地址，不再使用原来的控制器
                .usernameParameter("loginAcct")//指定页面参数
                .passwordParameter("userPswd")
                .defaultSuccessUrl("/fundingadmin/t-admin/admin/to/main/page.html")//登录成功页面
                .failureUrl("/fundingadmin/t-admin/admin/to/login/page.html?error=true")//登录失败
                .and()
                .logout()//启动退出功能
                .logoutUrl("/admin/security/logout")//指定退出处理地址，不再使用原来的控制器
                .logoutSuccessUrl("/fundingadmin/t-admin/index")
                .and()
                .csrf()
                .disable() // 禁用CSRF功能，如果不禁用，只能发post请求退出，否则报错
                .rememberMe()
                .rememberMeParameter("remember")
                ;

    }

    /**
     * 登录验证
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //内存测试  账号 test 密码123456 角色vip1
//        auth.inMemoryAuthentication()
//                .passwordEncoder(new BCryptPasswordEncoder())
//                .withUser("test")
//                .password(new BCryptPasswordEncoder()
//                .encode("123456"))
//                .roles("vip1");

        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
